package com.example.echoewish.ui.home_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.Comment;
import com.example.echoewish.pojo.DetailPost;
import com.example.echoewish.pojo.PartPost;
import com.example.echoewish.repository.CollectionRepository;
import com.example.echoewish.repository.CommentMessageRepository;
import com.example.echoewish.repository.PostMessageRepository;
import com.example.echoewish.util.LogUtil;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PostDetailViewModel extends ViewModel {

    MutableLiveData<Integer> postIdMutableLiveData;
    MutableLiveData<DetailPost> detailPostMutableLiveData;
    MutableLiveData<List<Comment>> commentMutableLiveData;
    MutableLiveData<String> responseMutableLiveData;
    MutableLiveData<String> collectResponseMutableLiveData;
    MutableLiveData<Comment> publishCommentMutableLiveData;
    MutableLiveData<Boolean> isCollectMutableLiveData;

    public PostDetailViewModel()
    {
        postIdMutableLiveData=new MutableLiveData<>();
        detailPostMutableLiveData=new MutableLiveData<>();
        commentMutableLiveData=new MutableLiveData<>();
        responseMutableLiveData=new MutableLiveData<>();
        publishCommentMutableLiveData=new MutableLiveData<>();
        isCollectMutableLiveData=new MutableLiveData<>();
        collectResponseMutableLiveData=new MutableLiveData<>();

    }

    public void getDetailPost()
    {
        new PostMessageRepository().queryDetailPost(postIdMutableLiveData.getValue(),new GetDetailPostCallBack());
    }

    public void publishComment()
    {
        CommentMessageRepository.getInstance().publishNewComment(publishCommentMutableLiveData.getValue(),new PublishCommentCallBack());
    }

    public void getComment()
    {
        CommentMessageRepository.getInstance().queryCommentByPostId(postIdMutableLiveData.getValue(),new GetCommentCallBack());
    }

    public void collectOrCancelPost()
    {
        if(!isCollectMutableLiveData.getValue())
        CollectionRepository.getInstance().collectPost(postIdMutableLiveData.getValue(), UserInfo.getInstance().UserId,new CollectPostCallBack());
        else
            CollectionRepository.getInstance().cancelCollection(postIdMutableLiveData.getValue(), UserInfo.getInstance().UserId,new CollectPostCallBack());
    }

    public void queryIsCollect()
    {
        if(UserInfo.getInstance().IsLogin)
        CollectionRepository.getInstance().queryIsCollect(postIdMutableLiveData.getValue(), UserInfo.getInstance().UserId,new QueryIsCollectCallBack());
    }

    private class GetDetailPostCallBack implements Callback<DetailPost>
    {

        @Override
        public void onResponse(Call<DetailPost> call, Response<DetailPost> response) {
            detailPostMutableLiveData.postValue(response.body());
        }

        @Override
        public void onFailure(Call<DetailPost> call, Throwable t) {
            detailPostMutableLiveData.postValue(null);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取帖子详情页 回调失败"+t);
        }
    }

    private class GetCommentCallBack implements Callback<List<Comment>>
    {

        @Override
        public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {
            commentMutableLiveData.postValue(response.body());
        }

        @Override
        public void onFailure(Call<List<Comment>> call, Throwable t) {
            commentMutableLiveData.postValue(null);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取评论 回调失败"+t);
        }
    }

    private class PublishCommentCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                responseMutableLiveData.postValue(response.body().string());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"发布评论 回调失败"+t);
        }
    }

    private class CollectPostCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                collectResponseMutableLiveData.postValue(response.body().string());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            collectResponseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"取消收藏 回调失败"+t);
        }
    }

    private class QueryIsCollectCallBack implements Callback<ResponseBody>
    {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                isCollectMutableLiveData.postValue(Boolean.valueOf(response.body().string()));
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            isCollectMutableLiveData.postValue(false);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取是否收藏 回调失败"+t);
        }
    }
}
